﻿
namespace HSS.Xaml.UI.Media.Animation
{
	#region Using Directives
	using System;
	using System.Windows;
	using System.Windows.Media.Animation;
	#endregion

	#region FrameworkElementFadeEffect
	/// <summary>
	/// Provides a programmatic fade effect on a FrameworkElement.
	/// </summary>
	public class FrameworkElementFadeEffect : FrameworkElementEffect
	{
		#region Fields
		double from;
		double to;
		double duration;
		#endregion

		#region Constructors
		/// <summary>
		/// Constructs the FadeEffect with the desired parameters.
		/// </summary>
		/// <param name="from">The value to begin the storyboard from.</param>
		/// <param name="to">The value to end the storyboard with.</param>
		/// <param name="duration">The time the fade runs</param>
		public FrameworkElementFadeEffect(double from, double to, double duration)
		{
			this.from = from;
			this.to = to;
			this.duration = duration;
		}
		#endregion

		#region Overrides
		/// <summary>
		/// Creates the desired StoryBoard.
		/// </summary>
		/// <param name="target">The FrameworkElement that will recieve the effect.</param>
		/// <returns>The created Storyboard.</returns>
		protected override Storyboard CreateStoryboard(FrameworkElement target)
		{
			var result = new Storyboard();

			var animation = new DoubleAnimation();
			animation.From = from;
			animation.To = to;
			animation.Duration = new Duration(TimeSpan.FromMilliseconds(duration));

			Storyboard.SetTarget(animation, target);
			Storyboard.SetTargetProperty(animation, new PropertyPath("Opacity"));

			result.Children.Add(animation);

			return result;
		}
		#endregion
	}
	#endregion
}